<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
    <meta charset="UTF-8">
    <title>欢迎页</title>

    {% include 'template/baseCss.volt' %}
    <link href="/css/vendor/common-grid.less" rel="stylesheet/less" type="text/css">
    <link href="/css/vendor/welcome-page.less" rel="stylesheet/less" type="text/css">
    <script src="/js/less.min.js"></script>
</head>

<body>
<!--  饼状图统计  -->
<div class="row pie-part">
    <div class="item-wrap">
        <div class="item">
            <table>
                <tr>
                    <td class="pie">
                        <canvas id="pie-mileage" width="60" height="60"></canvas>
                    </td>
                    <td class="name">本月里程<span class="value">0</span><br><span class="total">总里程:&nbsp;<span class="num">0</span></span></td>
                </tr>
            </table>
        </div>
    </div>
    <div class="item-wrap">
        <div class="item">
            <table>
                <tr>
                    <td class="pie">
                        <canvas id="pie-area" width="60" height="60"></canvas>
                    </td>
                    <td class="name">本月面积<span class="value">0</span><br><span class="total">总面积:&nbsp;<span class="num">0</span></span></td>
                </tr>
            </table>
        </div>
    </div>
    <div class="item-wrap">
        <div class="item">
            <table>
                <tr>
                    <td class="pie">
                        <canvas id="pie-duration" width="60" height="60"></canvas>
                    </td>
                    <td class="name">本月时长<span class="value">0</span><br><span class="total">总时长:&nbsp;<span class="num">0</span></span></td>
                </tr>
            </table>
        </div>
    </div>
    <div class="item-wrap">
        <div class="item">
            <table>
                <tr>
                    <td class="pie">
                        <canvas id="pie-flight-cnt" width="60" height="60"></canvas>
                    </td>
                    <td class="name">本月架次<span class="value">0</span><br><span class="total">总架次:&nbsp;<span class="num">0</span></span></td>
                </tr>
            </table>
        </div>
    </div>
</div>

<!--  图表统计  -->
<div class="row chart-part">
    <div class="item-wrap col-md-6 col-sm-12">
        <div class="tab">
            <div class="tag checked" id="sortie-tab"><span></span>月飞行架次统计</div>{% if user.isSystemAdmin %}<div class="tag" id="increase-tab"><span></span>月飞机增长统计</div>{% endif %}
        </div>
        <div class="item">
            <div class="chart-wrap">
                <canvas id="flight-cnt-chart" class="big-chart" height="380"></canvas>
                <canvas id="drone-cnt-chart" class="big-chart" height="380"  style="display: none"></canvas>
            </div>
        </div>
    </div>
    {% if user.isSystemAdmin %}
    <div class="item-wrap col-md-6 col-sm-12">
        <div class="tab">
            <div class="tag checked" id="camera-tab">月载荷统计</div><div class="tag" id="flight-tab">月架次统计</div>
        </div>
        <div class="item">
            <div class="chart-wrap">
                <div id="camera-pie" class="big-pie"></div>
                <div id="flight-pie" class="big-pie" style="visibility: hidden;"></div>
            </div>
        </div>
    </div>
    {% endif %}
</div>

<!-- 登录信息 -->
<div class="row login-info">
    <div class="item-wrap column">
        <div class="item">
            <p class="new-user">本月新增用户: <span class="cnt" id="user-add-count">0</span><span class="unit">名</span></p>
            <p class="info">您本月登录<span id="user-login-count">0</span>次 最近登录时间：<span id="user-login-time"></span></p>
        </div>
    </div>
</div>
</body>

{% include 'template/baseJs.volt' %}
<script src="/js/Chart.min.js"></script>
<script src="/third-part/echarts/echarts.min.js"></script>
<script src="/js/vendor/common.js"></script>
<script src="/js/vendor/chart-h.js"></script>
<script src="/js/vendor/welcome-page.js"></script>

<script>
    //标签切换
    $("#increase-tab").click(function () {
        $("#increase-tab").addClass("checked");
        $("#sortie-tab").removeClass("checked");
        $("#drone-cnt-chart").css("display", "block");
        $("#flight-cnt-chart").css("display", "none");
    });
    $("#sortie-tab").click(function () {
        $("#sortie-tab").addClass("checked");
        $("#increase-tab").removeClass("checked");
        $("#drone-cnt-chart").css("display", "none");
        $("#flight-cnt-chart").css("display", "block");
    });
    $("#flight-tab").click(function () {
        $("#flight-tab").addClass("checked");
        $("#camera-tab").removeClass("checked");
        $("#flight-pie").css("visibility", "visible");
        $("#camera-pie").css("visibility", "hidden");
    });
    $("#camera-tab").click(function () {
        $("#camera-tab").addClass("checked");
        $("#flight-tab").removeClass("checked");
        $("#flight-pie").css("visibility", "hidden");
        $("#camera-pie").css("visibility", "visible");
    });

    // 更新地图区域统计数据

    function addDate(date, days) {
        var d = new Date(date);
        d.setDate(d.getDate() + days);
        var m = d.getMonth() + 1;
        return d.getFullYear() + '-' + m + '-' + d.getDate();
    }
    var StatViewProxy = {
        totalData: false,
        curData: false,
        init: function () {
            loadingView.show();

            this.requestPlaneStat();
            this.requestFlightCountStat();
            this.requestUserAdd();
            this.requestLoginStatus();
            this.requestCameraMonthStat();
            this.requestFlightMonthStat();
            //最耗时,放在最后
            this.requestMonthStat();
        },
        requestLoginStatus: function () {


            feimaPost("/stat/doUserLoginStat", {}, function (result) {
                if (result.status == 0) {
                    var loginCount = result.data["login_count"];
                    var loginTime = result.data["last_login"];
                    if (loginCount != null) {
                        $("#user-login-count").text(loginCount);
                    }
                    if (loginTime != null) {
                        $("#user-login-time").text(loginTime);
                    }


                }

            });
        },
        requestUserAdd: function () {

            feimaPost("/stat/doMonthAddUser", {}, function (result) {
                $("#user-add-count").text(result.data);
            });
        },
        requestPlaneStat: function () {
            feimaPost("/stat/doNear6MonthPlaneStat", {}, function (result) {
                if (result.status == 0) {

                    var data = StatViewProxy._calNearSixMonthData(result.data);
                    // 更新月飞机增长率
                    var droneCntData = {
                        labels: data.label,
                        datasets: [
                            {
                                backgroundColor: "rgba(92,203,102,0.54)",
                                borderColor: "rgba(92,203,102,0.2)",
                                data: data.data
                            },
                        ],
                    };
                    welcomePageM.droneCntChart.update(droneCntData);
                }

            });
        },
        requestFlightCountStat: function () {

            feimaPost("/stat/doNear6MonthFlightCount", {}, function (result) {
                if (result.status == 0) {

                    var data = StatViewProxy._calNearSixMonthData(result.data);
                    // 更新月飞机增长率
                    var flightCntData = {
                        labels: data.label,
                        datasets: [
                            {
                                backgroundColor: "rgba(82,149,255,0.85)",
                                borderColor: "rgba(0,0,0,0)",
                                data: data.data
                            },
                        ],
                    };
                    welcomePageM.flightCntChart.update(flightCntData);
                }

            });

        },
        requestMonthStat: function () {
            feimaPost("/monitor/sysStatSearch", {}, function (result) {

                StatViewProxy.totalData = result.message;
                var startDate = new Date();
                var firstDate = new Date();
                firstDate.setDate(1);
                var endDate = new Date(firstDate);
                endDate.setMonth(firstDate.getMonth() + 1);
                endDate.setDate(0);
                var firstDateStr = firstDate.Format("yyyy-MM-dd");
                var endDateStr = endDate.Format("yyyy-MM-dd");
                feimaPost("/monitor/sysStatSearch", {
                    "startDate": firstDateStr,
                    "endDate": endDateStr
                }, function (result2) {
                    loadingView.remove();
                    StatViewProxy.curData = result2.message;
                    StatViewProxy.updatePieView();
                });
            });
        },
        updatePieView: function () {
            if (StatViewProxy.curData && StatViewProxy.totalData) {
                var cur = StatViewProxy.curData;
                var total = StatViewProxy.totalData;
                var pieData = {
                    mileage: {total: total.totalMil, zone: cur.totalMil},
                    area: {total: total.work_areas, zone: cur.work_areas},
                    duration: {total: total.work_times, zone: cur.work_times},
                    flightCnt: {total: total.totalMission, zone: cur.totalMission}
                };
                welcomePageM.pieBar.update(pieData);
            }

        },
        requestCameraMonthStat: function () {
            var firstDate = new Date();
            firstDate.setDate(1);
            var endDate = new Date(firstDate);
            endDate.setMonth(firstDate.getMonth() + 1);
            endDate.setDate(0);
            var firstDateStr = firstDate.Format("yyyy-MM-dd");
            var endDateStr = endDate.Format("yyyy-MM-dd");
            feimaPost("/monitor/sysCameraType", {
                "startDate": firstDateStr,
                "endDate": endDateStr
            }, function (result) {
                if (result.status == 0){
//                    var cameraCount = [];
                    //var cameraName = [];
                    var cameraData = [];
                    var data = JSON.parse(result.data.cameraType);
                    for (var i=0; i<data.length; i++){
//                        cameraCount.push(parseInt(data[i].total));
                        //cameraName.push(data[i].camera_type);
                        cameraData.push({value:parseInt(data[i].total), name: data[i].camera_type + ' [' + data[i].total + ']'})
                    }
                    welcomePageM.cameraPie.update(cameraData);
                }
            });
        },
        requestFlightMonthStat: function () {
            var firstDate = new Date();
            firstDate.setDate(1);
            var endDate = new Date(firstDate);
            endDate.setMonth(firstDate.getMonth() + 1);
            endDate.setDate(0);
            var firstDateStr = firstDate.Format("yyyy-MM-dd");
            var endDateStr = endDate.Format("yyyy-MM-dd");
            feimaPost("/monitor/sysPlaneModel", {
                "startDate": firstDateStr,
                "endDate": endDateStr
            }, function (result) {
                if (result.status == 0){
//                    var flightCount = [];
//                    var planeName = [];
                    var cameraData = [];
                    var data = JSON.parse(result.data.planeModel);
                    for (var i=0; i<data.length; i++){
//                        flightCount.push(parseInt(data[i].total));
//                        planeName.push(data[i].name);
                        cameraData.push({value:parseInt(data[i].total), name: data[i].name + ' [' + data[i].total + ']'})
                    }
                    welcomePageM.flightPie.update(cameraData);
                }
            });
        },
        _calNearSixMonthData: function (curData) {
            var sixMonth = StatViewProxy._getNearSixMonth();
            var data = new Array();
            for (var i = 0; i < sixMonth.length; i++) {

                var isFound = false;
                for (var j = 0; j < curData.length; j++) {
                    if (curData[j]["created_at"] == sixMonth[i]) {
                        isFound = true;
                        data.push(curData[j]["total"]);
                        break;
                    }
                }
                if (!isFound) {
                    data.push(0);
                }
            }
            return {
                label: sixMonth,
                data: data
            };
        },
        _getNearSixMonth: function () {
            var array = new Array();
            for (var i = 5; i >= 0; i--) {
                var curDate = new Date();
                curDate.setDate(1);
                curDate.setMonth(curDate.getMonth() - i);
                array.push(curDate.Format("yyyy-MM"));
            }
            return array;
        }

    };

    $(function () {

        StatViewProxy.init();
        //loadingView.remove();
    });
</script>


</html>






